package com.yxq.mdt.app.widgets;

import android.content.Context;
import android.media.MediaPlayer;

import com.alibaba.sdk.android.oss.OSS;
import com.alibaba.sdk.android.oss.OSSClient;
import com.alibaba.sdk.android.oss.common.auth.OSSCredentialProvider;
import com.alibaba.sdk.android.oss.common.auth.OSSPlainTextAKSKCredentialProvider;
import com.alibaba.sdk.android.oss.model.PutObjectRequest;

import java.io.File;
import java.io.IOException;

/**
 * oss服务类
 */

public class OssHelper {
    private static final String accessKeyId = "LTAI4FiY7us4vzLLArrKg8we";
    private static final String accessKeySecret = "WAPc9nzT3GpaY6TVyHxSI7Bq43OkXT";
    private static final String OSS_ENDPOINT = "https://oss-cn-hangzhou.aliyuncs.com/";
    //https://yxqoss.oss-cn-hangzhou.aliyuncs.com/yxq_wx/2019-10-08119.mp4
    private static String OSS_PREFIX_URL = "https://yxqoss.oss-cn-hangzhou.aliyuncs.com/";//阿里云地址前部分
    //image
    public static final String IMAGE = "image/";
    //头像
    public static final String AVATAR = "avatar/";
    //视频
    public static final String GS_APP = "gs_app/init/";
    public static final String GS_APP_VOICE = "gs_app/init/";

    /**
     * 鉴权提供
     */
    private static OSSCredentialProvider ossCredentialProvider;
    /**
     * OSS客户端
     */
    private static OSS oss;

    /**
     * 获取鉴权
     *
     * @return
     */
    private static OSSCredentialProvider getOSSCredentialProvider() {
        if (ossCredentialProvider == null) {
            ossCredentialProvider = new OSSPlainTextAKSKCredentialProvider(accessKeyId, accessKeySecret);
        }
        return ossCredentialProvider;
    }

    /**
     * 获取OSS客户端
     *
     * @param context
     * @return
     */
    public static OSS getOss(Context context) {
        if (oss == null) {
            oss = new OSSClient(context, OSS_ENDPOINT, getOSSCredentialProvider());
        }
        return oss;
    }


    /**
     * 获取OSS客户端
     *
     * @param context
     * @param list    是都是多图
     * @return
     */
    public static OSS getOss(Context context, boolean list) {
        return new OSSClient(context, OSS_ENDPOINT, getOSSCredentialProvider());
    }

    /**
     * 获取上传文件请求
     *
     * @param filePath
     * @return
     */
    public static PutObjectRequest getPutObjectRequest(String key, String filePath) {
        return new PutObjectRequest("yxqoss", key, filePath);
    }

    public static String getOssPrefixUrl(String key, String filePath) {
        return OSS_PREFIX_URL + key + filePath;
    }

    public static long getVideoVoiceTime(String fileTime) {
        File file = new File(fileTime);

        MediaPlayer mediaPlayer = new MediaPlayer();

        try {
            mediaPlayer.setDataSource(file.getPath());
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            mediaPlayer.prepare();
        } catch (IOException e) {
            e.printStackTrace();
        }

        long time = mediaPlayer.getDuration() / 1000;//获得了视频的时长（以毫秒为单位）
        return time;
    }


}
